package com.system.vote.core.captcha;

import cn.hutool.core.codec.Base64Encoder;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

public class CreateImageCode {
    public Map<String, String> getCodeString(int width, int height, int len) {
        SpecCaptcha captcha = new SpecCaptcha(width, height, len);
        captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        // 设置字体大小
        captcha.setCharType(Captcha.FONT_8);
        // 输出图片流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        captcha.out(byteArrayOutputStream);
        String str = "data:image/jpeg;base64,";
        String base64Img = str + Base64Encoder.encode(byteArrayOutputStream.toByteArray());

        Map<String, String> result = new HashMap<>();
        result.put("base64Img", base64Img);
        result.put("text", captcha.text().toLowerCase());
        return result;
    }
}
